﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Suffix
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_openDir_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folder = new FolderBrowserDialog())
            {
                folder.ShowNewFolderButton = false;
                folder.ShowDialog();
                this.txt_dir.Text = folder.SelectedPath;
            }
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            if (!this.txt_old.Text.StartsWith("."))
            {
                this.txt_old.Text = this.txt_old.Text.Insert(0, ".");
            }
            if (!this.txt_new.Text.StartsWith("."))
            {
                this.txt_new.Text = this.txt_new.Text.Insert(0, ".");
            }
            if (!Directory.Exists(txt_dir.Text))
            {
                MessageBox.Show("目录不存在");
                return;
            }
            string[] files = Directory.GetFiles(txt_dir.Text, "*" + txt_old.Text);
            if (files.Count() > 0)
            {
                foreach (var item in files)
                {
                    string newFile = item.Replace(this.txt_old.Text, this.txt_new.Text);
                    File.Move(item, newFile);
                }
                MessageBox.Show("修改完成");
            }
            else
            {
                MessageBox.Show("未找到文件");
            }
        }
    }
}
